package com.dingdang.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@EnableAsync
@Configuration
public class ThreadPoolConfig {

    @Bean
    public Executor tarkExecutor(){
        //spring提供的连接池
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //设置工作核心工作线程数
        executor.setCorePoolSize(10);
        //设置非核心最大工作线程数
        executor.setMaxPoolSize(100);
        //设置线程池等待队列
        executor.setQueueCapacity(100);
        //设置线程池的拒绝策略      这个配置的是不断重试
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //设置前缀的前缀名
        executor.setThreadNamePrefix("dingdangcar-freemarker-");
        //初始化线程数
        executor.initialize();

        return executor;
    }
}
